package com.example.jniexample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    private val fieldA: Int = 10

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Example of a call to a native method
        findViewById<TextView>(R.id.sample_text).text = stringFromJNI()
        Log.i(TAG, "accessFieldA() : ${accessFieldA()}")
        Log.i(TAG, "writeFieldA(123)")
        writeFieldA(123)
        Log.i(TAG, "accessFieldA() : ${accessFieldA()}")
        Log.i(TAG, "accessStaticFieldB() : ${accessStaticFieldB()}")
        Log.i(TAG, "writeStaticFieldB(\"xixi\")")
        writeStaticFieldB("xixi")
        Log.i(TAG, "accessStaticFieldB() : ${accessStaticFieldB()}")
        Log.i(TAG, "invokeStaticMethod() : ${invokeStaticMethod("jni")}")
        Log.i(TAG, "invokeMethod() : ${invokeMethod(1,2)}")
    }

    private fun method(a: Int, b: Int): Int {
        return a + b
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String
    external fun dynamicRegister(): String
    external fun accessFieldA(): Int
    external fun writeFieldA(i: Int)
    external fun accessStaticFieldB(): String
    external fun writeStaticFieldB(s: String)
    external fun invokeStaticMethod(str: String): String
    external fun invokeMethod(a: Int, b: Int): Int

    companion object {
        // Used to load the 'native-lib' library on application startup.
        const val TAG: String = "MainActivity"
        const val staticFieldB: String = "haha"

        init {
            System.loadLibrary("native-lib")
        }

        @JvmStatic
        private fun staticMethod(str: String): String {
            Log.i(TAG, "staticMethod()")
            return "hello! $str"
        }
    }
}